package com.chinairi.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chinairi.common.annotation.DoPage;
import com.chinairi.common.utils.UserSession;
import com.chinairi.mall.controller.viewobject.CollectVO;
import com.chinairi.mall.error.BusinessException;
import com.chinairi.mall.error.EmBusinessError;
import com.chinairi.mall.model.Collect;
import com.chinairi.mall.dao.CollectMapper;
import com.chinairi.mall.service.ICollectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 收藏夹表 服务实现类
 * </p>
 *
 * @author Webgasd
 * @since 2020-09-05
 */
@Service
public class CollectServiceImpl extends ServiceImpl<CollectMapper, Collect> implements ICollectService {
    @Autowired
    private CollectMapper collectMapper;

    /**
     * 添加一个收藏
     *
     * @param collect
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(Collect collect) {
        String userId = UserSession.getUserId();
        if(StringUtils.isEmpty(userId)){
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR,"用户token无效");
        }
        String uuid = UUID.randomUUID().toString().replace("-", "");
        collect.setcId(uuid);
        collect.setcUserCode(UserSession.getUserId());
        collect.setcTime(new Date());
        collectMapper.insert(collect);
    }

    /**
     * 删除某个收藏
     *
     * @param uuid
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(String uuid) {
        Collect result = collectMapper.selectById(uuid);
        if (result == null) {
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR, "无此条目，请刷新后再试");
        }
        collectMapper.deleteById(uuid);
    }

    /**
     * 删除某个收藏
     *
     * @param goodsDbId
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteGoods(String goodsDbId) {
        QueryWrapper<Collect> wrapper = new QueryWrapper<>();
        wrapper
                .eq(StringUtils.isNotBlank(goodsDbId), "C_GOODS_CODE", goodsDbId)
                .eq(StringUtils.isNotBlank(UserSession.getUserId()), "C_USER_CODE", UserSession.getUserId());
        collectMapper.delete(wrapper);
    }

    /**
     * 删除某个收藏
     *
     * @param CsDbId
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteShop(String CsDbId) {
        QueryWrapper<Collect> wrapper = new QueryWrapper<>();
        wrapper
                .eq(StringUtils.isNotBlank(CsDbId), "C_SHOP_CODE", CsDbId)
                .eq(StringUtils.isNotBlank(UserSession.getUserId()), "C_USER_CODE", UserSession.getUserId())
                .isNull("C_GOODS_CODE");
        collectMapper.delete(wrapper);
    }

    /**
     * 获取账号下所有的收藏夹内容，分为两个按钮
     *
     * @param collect
     * @param flag    0:查看收藏的商品，1:查看收藏的店铺
     * @return
     */
    @Override
    @DoPage
    public List<CollectVO> get(Collect collect, Integer flag) {
        collect.setcUserCode(UserSession.getUserId());
        if (flag == 0) {
            return collectMapper.getListGoods(collect);
        } else if (flag == 1) {
            return collectMapper.getListStore(collect);
        } else {
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR, "参数错误，无法获取收藏夹信息");
        }
    }
}
